var app = require('electron').remote;
const path = require('path')
const url = require('url');
var Menu = app.Menu;
const BrowserWindow = app.BrowserWindow
let mainWindow = null;
const ipc = app.ipcMain;
const appPath = path.join(app.app.getPath('appData') + './Requester');

// const dbConfig = require(path.join(__dirname, '../proxy/dbConfig.js'));
function upDateMenu(template) {
    var menu = Menu.buildFromTemplate(template);
    Menu.setApplicationMenu(menu);
};

var changeConfig = function (event, arg) {
    mainWindow.close();
    location.reload();
};
ipc.on('change-config', changeConfig);

window.createWindow = function () {
    if (mainWindow) {
        mainWindow.focus();
    } else {
        // const modalPath = path.join(__dirname, '../portal/win/DBConfig/index.html');
        mainWindow = new BrowserWindow({
            width: 897,
            height: 768,
            icon: path.join(__dirname, '../portal/app.png'),
            title: 'DBConfig-Requester',
            webPreferences: {
                nodeIntegration: true,
            }
            // resizable: false,
        });
        mainWindow.setMenu(Menu.buildFromTemplate([]));
        mainWindow.setMenu(Menu.buildFromTemplate([{
            label: '调试工具',
            accelerator: 'F12',
            click: function (item, focusedWindow) {
                if (focusedWindow)
                    focusedWindow.toggleDevTools();
            }
        }, {
            label: '重载窗口',
            accelerator: 'F5',
            click: function (item, focusedWindow) {
                if (focusedWindow)
                    focusedWindow.reload();
            }
        }]));
        mainWindow.loadURL(url.format({
            pathname: path.join(__dirname, '../portal/win/DBConfig/index.html'),
            protocol: 'file:',
            slashes: true,
            skipTaskbar: false,
        }));
        mainWindow.show();
        mainWindow.on('close', function () {
            mainWindow = null;
        });
    };
};

var template = [{
    label: '文件',
    submenu: [{
        label: '重载窗口',
        accelerator: 'F5',
        click: function (item, focusedWindow) {
            if (focusedWindow)
                focusedWindow.reload();
        }
    }, {
        label: '调试工具',
        accelerator: 'F12',
        click: function (item, focusedWindow) {
            if (focusedWindow)
                focusedWindow.toggleDevTools();
        }
    }, {
        label: '保存',
        accelerator: 'CmdOrCtrl+s',
        click: function (item, focusedWindow) {
            SaveEvent();
        }
    }, {
        label: '重命名',
        accelerator: 'F2',
        click: function (item, focusedWindow) {
            ReName();
        }
    }, {
        label: '打开数据文件所在目录',
        click: function () {
            app.shell.openExternal(appPath);
        }
    }, {
        label: '项目管理',
        click: function () {
            createWindow()
        }
    }],
}, {
    label: '编辑',
    submenu: [{
        label: '撤销',
        accelerator: 'CmdOrCtrl+Z',
        role: 'undo'
    }, {
        label: '重做',
        accelerator: 'Shift+CmdOrCtrl+Z',
        role: 'redo'
    }, {
        type: 'separator'
    }, {
        label: '剪切',
        accelerator: 'CmdOrCtrl+X',
        role: 'cut'
    }, {
        label: '复制',
        accelerator: 'CmdOrCtrl+C',
        role: 'copy'
    }, {
        label: '粘贴',
        accelerator: 'CmdOrCtrl+V',
        role: 'paste'
    }, {
        label: '全选',
        accelerator: 'CmdOrCtrl+A',
        role: 'selectall'
    }]
}];
upDateMenu(template);

// , {
//     label: '数据目录',
//     click: function () {
//         app.shell.openExternal(path.join(__dirname, '../../userDB'));
//     }
// }

// {
//     label: '资源目录',
//     click: function () {
//         app.shell.openExternal(path.join(__dirname, './data'));
//     }
// }

// 打开文件夹
// app.shell.openExternal(path.join(__dirname,'../'));

// 打开连接
// require('electron').shell.openExternal('http://electron.atom.io');